package com.luhai.shipmentscheck.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luhai.shipmentscheck.R;
import com.luhai.shipmentscheck.adapter.ClIdAdapter;
import com.luhai.shipmentscheck.config.Constants;
import com.luhai.shipmentscheck.entity.Info;
import com.luhai.shipmentscheck.manager.PermissionManager;
import com.luhai.shipmentscheck.util.OkHttpUtils;
import com.luhai.shipmentscheck.widget.TopBar;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class InputActivity extends BaseActivity implements TextWatcher, OkHttpUtils.OkHttpCallBackLinener, AdapterView.OnItemClickListener, TopBar.OnTopBarClickListener {


    private EditText et_cl_id;

    private ListView lv_cl_ids_like_search;

    private List<String> mList = new ArrayList<>();

    //private ImageView iv_enter;

    private Button btn_enter;

    private TopBar topBar;

    private ClIdAdapter clIdAdapter;

    private long lastTextChangedTime;

    private Handler mHandler;


    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.btn_enter:
                Intent intent = new Intent(InputActivity.this, MainActivity.class);
                intent.putExtra("cl_id", et_cl_id.getText().toString());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void init() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0x10:
                        OkHttpUtils.getInstance().doGet(Constants.URL_GET_CL_ID_LIKE_LIST + "?cl_id" + msg.obj.toString(), InputActivity.this);
                        break;
                }
            }
        };
        new PermissionManager(this, new PermissionManager.PermissionInterface() {
            @Override
            public int getPermissionsRequestCode() {
                return 0;
            }

            @Override
            public String[] getPermissions() {
                return new String[]{Manifest.permission.CAMERA, Manifest.permission.INTERNET};
            }

            @Override
            public void requestPermissionsSuccess() {

            }

            @Override
            public void requestPermissionsFail() {

            }
        }).requestPermissions();
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_input;
    }

    @Override
    public void initView(View view) {

        topBar = findViewById(R.id.topBar);
        topBar.setVisible(true, false);
        et_cl_id = findViewById(R.id.et_cl_id);
        btn_enter = findViewById(R.id.btn_enter);
        lv_cl_ids_like_search = findViewById(R.id.lv_cl_ids_like_search);
        clIdAdapter = new ClIdAdapter(mList, this);
        lv_cl_ids_like_search.setAdapter(clIdAdapter);
    }

    @Override
    public void setListener() {

        btn_enter.setOnClickListener(this);
        et_cl_id.addTextChangedListener(this);
        lv_cl_ids_like_search.setOnItemClickListener(this);
        topBar.setOnTopBarClickListener(this);
    }

    @Override
    public void doBusiness(Context mContext) {
        //OkHttpUtils.getInstance().setTimeOut(2,2,2);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        String str = et_cl_id.getText().toString();
        mHandler.removeMessages(0x10);
        if (!"".equals(str) && str != null) {
            Message message = Message.obtain();
            message.what = 0x10;
            message.obj = str;
            mHandler.sendMessageDelayed(message, 2 * 1000);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void failure(Exception e) {

    }

    @Override
    public void success(String json) {

        Log.i(TAG, "获取到的JSON：" + json);
        mList.clear();
        try {

            Gson gson = new Gson();
            List<Map<String, String>> list = gson.fromJson(json, new TypeToken<List<Map<String, String>>>() {
            }.getType());
            for (Map<String, String> map : list) {
                mList.add(map.get("CL_ID"));
                Log.i(TAG, "转换成的INFO对象为：" + map.get("CL_ID").toString());
            }
            clIdAdapter.notifyDataSetChanged();
        } catch (Exception e) {

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        et_cl_id.setText(mList.get(position));
        /*Intent intent = new Intent(InputActivity.this, MainActivity.class);
        intent.putExtra("cl_id", et_cl_id.getText().toString());
        startActivity(intent);*/
    }

    @Override
    public void onTopBarClick(boolean isLeft) {

        if (!isLeft) {
            startActivity(SettingActivity.class);
        }
    }
}
